package XML;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.File;

public class XMLUtils {
    public static String getStringFromXml(String name) {

        try {
            DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = bdf.newDocumentBuilder();
            Document doc = (Document) documentBuilder.parse(new File("./config.xml"));

            NodeList nodeList = doc.getElementsByTagName(name);
            Node node = nodeList.item(0).getFirstChild();
            return node.getNodeValue().trim();

        } catch (Exception e) {
            System.out.println("Config can't find !!!");
            e.printStackTrace();
        }
        return null;

    }

    public static Object getBeanFromXml(String name){
        try {
            Class clazz =Class.forName(getStringFromXml(name));
            return clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
